<%--
  Created by IntelliJ IDEA.
  User: 李昭富
  Date: 2024/5/31
  Time: 16:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<%@ page import = "java.sql.*" %>
<%!
    //建立连接对象
    Connection getConnection(){
        //1、加载驱动
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        //2、获得连接
        String dsn="jdbc:mysql://localhost:3306/javaweb";
        String uname="root";
        String pwd="Li021123";
        Connection conn=null;
        try{
            conn=DriverManager.getConnection(dsn,uname,pwd);
        }catch(SQLException e){
            System.out.println(e.getMessage());
        }
        return conn;
    }
    //释放资源
    void closeDB(Connection conn,Statement stmt,ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
            }
            if(stmt!=null){
                stmt.close();
            }
            if(conn!=null){
                conn.close();
            }
        }catch(SQLException e){
            System.out.println(e.getMessage());
        }

    }
%>